<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/5/14
 * Time: 15:25
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class  Part extends Base {
    /**
     * 提现列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $part_name= input('part_name');
            $user_username = input('user_username');
            $phone = input('phone');
            $withdraw_status = input('withdraw_status');
            $where = [];
            if(isset($phone) && $phone != "")
            {
                $where['uuu.user_phone'] = ['like', "%" . $phone . "%"];
            }
            if(isset($user_username) && $user_username != "")
            {
                $where['uuu.user_username'] = ['like', '%' . $user_username . '%'];
            }
            if(isset($part_name) && $part_name != "")
            {
                $where['a.part_name'] = ['like', '%' . $part_name . '%'];
            }
            if(isset($withdraw_status) && $withdraw_status != "")
            {
                $where['a.p_status'] = $withdraw_status;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_part')->alias('a')
                ->join('up_u_user uuu', 'uuu.user_id = a.p_user_id', 'left')
                ->join('up_part_theme uph', 'uph.part_theme_id = a.p_theme', 'left')
                ->join('up_reward ur', 'ur.reward_id = a.p_reward', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_part')->alias('a')
                ->join('up_u_user uuu', 'uuu.user_id = a.p_user_id', 'left')
                ->join('up_part_theme uph', 'uph.part_theme_id = a.p_theme', 'left')
                ->join('up_reward ur', 'ur.reward_id = a.p_reward', 'left')
                ->where($where)
                ->field('uuu.user_username,uuu.user_phone,uph.part_theme_name,ur.reward_price
                ,a.*')
                ->page($NowPage,$limits)
                ->order('p_status asc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(session('?member_id')){
            session('member_id',null);
        }
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 编辑
     */
    public function update(){
        if(Request::instance()->isPost()){
            $id = input('id');
            $data['part_name'] = input('param.part_name');
            $data['p_theme'] = input('param.p_theme');
            $data['p_address'] = input('param.p_address');
            $data['p_start_time'] = input('param.start');
            $res = Db::table('up_part')->where('part_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $id = input('part_id');
        $where['a.part_id'] = $id;
        //查询
        $info = Db::table('up_part')->alias('a')
            ->join('up_u_user uuu', 'uuu.user_id = a.p_user_id', 'left')
            ->join('up_part_theme uph', 'uph.part_theme_id = a.p_theme', 'left')
            ->join('up_reward ur', 'ur.reward_id = a.p_reward', 'left')
            ->where($where)
            ->field('uuu.user_username,uuu.user_phone,uph.part_theme_name,ur.reward_price
                ,a.*')
            ->find();
        //查询主题
        $info1 = Db::table('up_part_theme')->select();
        $this->assign('id',$id);
        $this->assign('res',$info);
        $this->assign('res1',$info1);
        return $this->fetch();
    }

    public function member(){
        if(request()->isAjax()){
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $where['a.part_id'] = session('member_id');
            $where['a.m_status'] = 2;
            $where['a.is_owner'] = 1;
            $count = Db::table('up_part_member')->alias('a')
                ->join('up_u_user uuu', 'uuu.user_id = a.m_user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_part_member')->alias('a')
                ->join('up_u_user uuu', 'uuu.user_id = a.m_user_id', 'left')
                ->field('uuu.user_username,uuu.user_phone,a.*')
                ->where($where)
                ->page($NowPage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(!session('?member_id')){
            session('member_id',input('part_id'));
        }
        return $this->fetch();
    }
}